/*
 * @Author: Jiraiya
 * @Date: 2020-06-09 15:47:46
 * @LastEditors: Jiraiya
 * @LastEditTime: 2020-06-09 19:26:48
 * @Description: 
 */

const pluginName = 'ConsoleStartBuild';
const fs = require('fs-extra');
class ConsoleStartBuild{
    constructor(options){
        this.options = options;
    }

    apply(compiler){
        const self = this;
        compiler.plugin('wwwwww', function(compilation, cb){
            console.log('测试系列');
            cb();
        })
        compiler.hooks.run.tapAsync(pluginName, (compilation,cb) => {
            console.log('构建开始');
            fs.writeJSONSync('./1.json', compiler);
            console.log('构建开始');
            cb();
        })
        compiler.hooks.emit.tapAsync('sourceOutputBefore',(compilation, cb) => {
            console.log('开始将生成的资源写入dist');
            const newVal = JSON.stringify(compilation, (function(){
                const store = new WeakSet();
                return function(key, val){
                    if(typeof val === 'object' && val !== null){
                        if(store.has(val)) return;
                        store.add(val);
                    }
                    return val;
                }
            })())
            const res = [];
            for(let key in compilation){
                if(compilation.hasOwnProperty(key)){
                    res.push(key);
                }
            }
            // console.log(JSON.stringify(res));
            fs.writeJSONSync('./2.json', newVal);
            cb();
        })
    }
}
module.exports = ConsoleStartBuild;